package com.cskaoyan.market.admin.controller;

import com.cskaoyan.market.admin.service.MarketAdminCommentService;
import com.cskaoyan.market.admin.service.impl.MarketAdminCommentServiceImpl;
import com.cskaoyan.market.db.domain.MarketComment;
import com.cskaoyan.market.util.HttpUtil;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author James
 * @since 2023/03/21 11:27
 */
@WebServlet("/admin/comment/*")
public class AdminCommentController extends HttpServlet {

    MarketAdminCommentService commentService = new MarketAdminCommentServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String replace = req.getRequestURI().replace(req.getContextPath() + "/admin/comment/", "");
        if ("list".equals(replace)) {
            list(req, resp);
        }
    }

    //展示评论
    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String pageParam = req.getParameter("page");
        String limitParam = req.getParameter("limit");
        String userId = req.getParameter("userId");
        String valueIdParam = req.getParameter("valueId");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        //校验
        if (StringUtils.isEmpty(pageParam) || StringUtils.isEmpty(limitParam)) {
            //这两个参数都要求必须是有的
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        //关于sort、order正常情况下应该是需要有的，如果没有传过来，那么设置一个默认值
        if (StringUtils.isEmpty(sort)) {
            sort = "add_time";
        }
        if (StringUtils.isEmpty(order)) {
            order = "desc";
        }

        int page = 0;
        int limit = 0;
        int valueId = 0;
        try {
            //必须是整数
            page = Integer.parseInt(pageParam);
            limit = Integer.parseInt(limitParam);
            if (!StringUtils.isEmpty(valueIdParam)) {
                valueId = Integer.parseInt(valueIdParam);
            }
        } catch (NumberFormatException e) {
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        ////调用service
        List<MarketComment> commentsList = commentService.list(page, limit, userId, valueId, sort, order);
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.okList(commentsList)));
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String replace = req.getRequestURI().replace(req.getContextPath() + "/admin/comment/", "");
        if ("delete".equals(replace)) {
            delete(req, resp);
        }
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {

        String requestBody = HttpUtil.getRequestBody(request);
        String addTime = JacksonUtil.parseString(requestBody, "addTime");
        String content = JacksonUtil.parseString(requestBody, "content");
        String id = JacksonUtil.parseString(requestBody, "id");
        String updateTime = JacksonUtil.parseString(requestBody, "updateTime");
        String star = JacksonUtil.parseString(requestBody, "star");
        String valueId = JacksonUtil.parseString(requestBody, "valueId");
        if (StringUtils.isEmpty(content)) {
            response.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        //调用Service
        commentService.delete(addTime, content, id, updateTime, star, valueId);
        response.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok()));


    }


}










